<template>
	<view class="padding30">
		<view class="flex row-column row-center col-center mt252" v-if="addlist.length<1">
			<view class="" style="border: 2rpx dashed #ccc;">
				<image src="../../static/image/my/noAdd.png" class="w400 h374"></image>
			</view>
		</view>
		<view class="bg-f pl20 pt30 pb20 pr20 radius8 mb30"  @click="gotoback(item)" v-else v-for="(item,i) in addlist" :key='i'>
			<view class="solid-bottom pb30">
				<view class="flex col-center mb20">
					<view class="fs19 w58 h32 flex row-center col-center radius4 color-f mr12"
						style="background-color: #FF6B35;" v-if='item.is_default==1'>
						默认
					</view>
					<text class="mr27 fw-500">{{item.name}}</text>
					<text class="fw-500">{{item.phone}}</text>
				</view>
				<text class="color6 fs27">{{item.region}}{{item.addr}}</text>
			</view>
			<view class="fs27 flex row-between col-center mt21 ">
				<view class="flex col-center">
					<radio-group>
						<radio :checked="item.id==is_id"  @click.stop="change(item)" color="#FF6B35"/>
					</radio-group>
					<text class="">设为默认地址</text>
				</view>
				<view class="">
					<text class="mr65" @click.stop='editAdd(item)'>编辑</text>
					<text @click.stop="deleteAdd(item)">删除</text>
				</view>
			</view>
		</view>
		
		<!-- 底部按钮 -->
		<view class="fw-500 footerBtn pl30 pr30 bg-f pt20 pb70">
			<u-button type="primary" shape="circle" @click='editAdd'>创建地址</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad(opt) {
			if(opt.from) {
				this.from=opt.from
			}
		},
		onShow() {
			this.getAddList()
		},
		data() {
			return {
				addlist:[],
				is_id:'',
				from:''
			};
		},
		methods:{
			getAddList() {
				this.$api.addlist().then(res=>{
					this.addlist=res.data.data
					let arr=[]
					arr = res.data.data.filter(item=>{
						return item.is_default==1
					})
					this.is_id=arr[0].id
				})
			},
			//新增/编辑地址
			editAdd(item) {
				if(item.id) {
					uni.setStorageSync('address',item)
					this.$jump(`/pages/my/edit_address?id=${item.id}`)
				}else {
					this.$jump(`/pages/my/edit_address`)
				}
			},
			//删除地址
			deleteAdd(item) {
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '确认删除地址?',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
							that.$api.do_delete({id:item.id}).then(res=>{
								that.$msg(res.msg)
								that.getAddList()
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			//设置默认地址 
			change(item) {
				this.is_id=item.id
				this.$api.set_default({id:item.id}).then(res=>{
					this.$msg(res.msg)
					this.getAddList()
				})
			},
			gotoback(item) {
				if(this.from==1) {
					uni.$emit('update', JSON.stringify(item))
					uni.navigateBack()
				}
			}
				
		},
	}
</script>

<style lang="scss">

</style>
